package dataDraw.classDiagramDraw;

import data.DataVector;
import data.Position;
import data.classDiagram.*;
import dataDraw.Constants;
import dataDraw.TextDraw;

import java.awt.*;

/**
 * User: Szymon Kudzia
 * Date: 22.04.13
 * Time: 15:59
 */
public class GradientBackgroundEntityDraw extends BasicEntityDraw {
	protected Color startColor;
	protected Color endColor;

	public void draw(Entity entity, Graphics graphics) {
		this.graphics = graphics;
		textDraw.setGraphics(graphics);

		drawBackground(entity, graphics);

		super.draw(entity, graphics);
	}

    protected void drawBackground(Entity entity, Graphics graphics1)
    {
        Point size = calculateSize(entity, graphics);

        Graphics2D g2d = (Graphics2D)graphics;

        int posX = entity.getPosition().getX();
        int posY = entity.getPosition().getY();

        GradientPaint dp = new GradientPaint(posX, posY, startColor,
                posX + size.x, posY + size.y, endColor);
        g2d.setPaint(dp);

        g2d.fillRect(entity.getPosition().getX(),
                entity.getPosition().getY(), size.x, size.y);
    }

    protected Point calculateSize(Entity entity, Graphics graphics) {
        int height = (1 + entity.getMembers().size() + entity.getMethods().size());
        int numberOfMargines = 6;

        if (entity instanceof ClassEntity) {
            int tn = ((ClassEntity)entity).getTriggers().size();
            height += tn;

            numberOfMargines += tn > 0 ? 2 : 0;
        }

        height = height * graphics.getFontMetrics().getHeight() +
                numberOfMargines * Constants.MARGIN;// + height * lineSpacing;

        int width = graphics.getFontMetrics().stringWidth(entity.getName());

        for (Member m : entity.getMembers())
            width = Math.max(width, graphics.getFontMetrics().stringWidth(m.toString()));

        for (Method m : entity.getMethods())
            width = Math.max(width, graphics.getFontMetrics().stringWidth(m.toString()));

        if (entity instanceof ClassEntity) {
            for (Trigger t : ((ClassEntity)entity).getTriggers())
                width = Math.max(width, graphics.getFontMetrics().stringWidth(t.toString()));
        }

	    /*if (width == 0)
		    width =  entity.getPosition().getWidth();
	    else                                           */
	        width = width + 2 * Constants.MARGIN;

	    return new Point(width, height);
    }

	public void setEndColor(Color endColor) {
		this.endColor = endColor;
	}

	public void setStartColor(Color startColor) {
		this.startColor = startColor;
	}
}
